var rootPath = getRootPath();

$(function() {
	//滚动条置顶
	 $("body").prepend('<input type="text" id="focus" size="1">');
    $("#focus").focus();
   $("#focus").hide();
	var rleId = getUrlParam("rleId");
	if(rleId == undefined || rleId == 'undefined' || rleId == '') {
		layer.alert("认证出错,请求参数有误:)");
	}
	$("#rleId").val(rleId);
	var prevPage = getUrlParam("currentPage");
	$("#gid").css("cursor", "pointer");
	
	var sid = '';
	var gid;
	$.ajax({
		url: rootPath + '/verify/uid/rleId/' + rleId + '/dr/1',
		type: 'GET',
		success: function(data) {
			if(data.returnCode == '0000') {
				var obj = data.returnObject;
				sid = obj.school.sid;
				$("#school").html("<option value='" + obj.school.sid + "'>" + obj.school.sname + "</option>");
				$("#subject").val(obj.subject);
				gid = obj.grade.gid;
				$("#gid").find("option[value='" + gid + "']").prop("selected","selected");
				$("#job").val(obj.job);
				$("#eduAge").val(obj.eduAge);
				$("#intro").val(obj.intro);
			}
			
//			$.ajax({
//				url: rootPath + '/school/dr/1',
//		    	type: 'GET',
//		    	async : false,
//		    	success: function (data) {
//		    		if(data.returnCode == "0000") {
//		    			var obj = data.returnObject;
//		    			var length = obj.length;
//		    			var option = "<option value='' selected='selected'>请选择学校</option>"
//		    			for(var i = 0;i < length;i++) {
//		    				option += "<option value='" + obj[i].sid + "'>" + obj[i].sname + "</option>"
//		    			}
//		    			$("#school").append(option);
//		    			$("#school").select2();
//		    			$("#school").select2('val',sid);
//		    		} else {
//		    			layer.alert(data.returnMessage);
//		    		}
//		    	},
//		    	error: function (jqXHR, textStatus, errorThrown) {
//		    		layer.alert("error:(");
//		    	}
//			});
		},error: function (jqXHR, textStatus, errorThrown) {
    		layer.alert("error:(");
    	}
	});
	setTimeout(function(){
	$("#school").select2({
		ajax:{
			type: 'POST',
			url: rootPath + '/solr/schoolSearch',
			delay: 250,
			data: function (params) {
		          return {
		            text: params.term, // search term 请求参数
		            page: params.page
		          };
		        },
	        processResults: function (data, params) {
	            params.page = params.page || 1;
	            var srcList = data.returnCode=="0000"?data.returnObject:[];
	            var desList = [];
	            var temp;
	            for(var i=0;i<srcList.length;i++){
	            	temp = {};
	            	temp.id=srcList[i].id;
	            	temp.text=srcList[i].title[0];
	            	desList[i]=temp;
	            }
	            
	            return {
	              results: desList,//itemList
	              pagination: {
	                more: (params.page * 2) < data.total_count
	              }
	            };
	          },
	          cache: true
	        },
	        placeholder:'请选择学校',//默认文字提示
	        language: "zh-CN",
	        tags: false,//允许手动添加
	        allowClear: true,//允许清空
	        escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
	        minimumInputLength: 2,
	        formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
	        formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
	});
	},50);
	
	$("#intro").bind("change,keydown,up", function() {
		$(this).val($(this).val().substring(0,800));
	});
	
	//上一步
	$("#prev").click(function() {
		var creType = $("#sort").val();
		parent.$("#content").prop("src", prevPage + "?rleId=" + rleId + "&creType=" + creType);
	});
	
	$("#applyAuth").click(function() {
		if(!checkData()){
			return;
		}
		
		//所教项目
		if($("#subject").val().length > 50) {
			$("#subject").focus();
			showErrMsg("subject", "所交科目输入太长,请精简");
			return;
		}
		
		//职务
	/*	if($("#job").val().length > 50) {
			$("#job").focus();
			showErrMsg("job", "职务输入太长,请精简")
			return;
		}*/
		
		//简介
		if($("#intro").val().length > 800) {
			$("#intro").focus();
			showErrMsg("intro", "简介输入太长,请精简")
			return;
		}
		
		showLoading();
		$("#teacherAuthForm").ajaxSubmit({
			url : rootPath + '/verify/saveOrUpdateVerify',
			type: 'POST',
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if(data.returnCode == '0000'){
					layer.msg("操作成功",{icon:1,time:1000},function() {
						var callbackUrl = getUrlParam("callbackUrl");//getUrlParam 带转义
						if(callbackUrl){
							parent.document.location.href = callbackUrl;
						}else{
							parent.$("#content").prop("src","applyResult.html?rleId=" + rleId + "&title=梦想教师认证");
						}
					});
				}else{
					layer.alert(data.returnMessage);
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				hideLoading();
				layer.alert("error");
			}
		});
		return false;
	});
});

function checkData(){
	//var sid = $("#school").select2('val');
	var sid=$("#school option:selected").val();
	if($.trim(sid) == '') {
		$("#school").focus();
		showErrMsg("school","请选择学校");
		return false;
	}
	if($.trim($("#subject").val()) == '') {
		$("#subject").focus();
		showErrMsg("subject","请输入所教科目");
		return false;
	}
	if($.trim($("#job").val()) == '') {
		$("#job").focus();
		showErrMsg("job","请选择职务");
		return false;
	}
	if($.trim($("#intro").val()) == '') {
		$("#intro").focus();
		showErrMsg("intro","请输入简介");
		return false;
	}
	return true;
}